import Vue from 'vue'
import Vuex from 'vuex'
import moneyData from "./moneyData.js"
import tomatoData from "./tomatoData.js"
import now from "../apis/now.js"

Vue.use(Vuex)

const store = new Vuex.Store({
	modules: {
		moneyData,
		tomatoData
	},
	state: {
		todolist: [],
		count: 0
	},
	getters: {
		setTodolist(state) {
			return state.todolist.filter((item) => {
				return item.date === now()
			})
		}
	},
	mutations: {
		getTodolist(state) {
			state.todolist = JSON.parse(uni.getStorageSync('todolist') || '[]')
		},
		addTodolist(state, payload) {
			if (payload === undefined) {
				return
			} else {
				this.commit("getTodolist")
				state.todolist.push({
					date: now(),
					note: payload
				})
				uni.setStorageSync('todolist', JSON.stringify(state.todolist))
			}
		},
		setTodolist(state, payload) {
			this.commit("getTodolist")
			state.todolist[payload.index].note = payload.newTodo
			uni.setStorageSync('todolist', JSON.stringify(state.todolist))

		},
		delTodolist(state, payload) {
			this.commit("getTodolist")
			state.todolist.splice(payload, 1)
			uni.setStorageSync('todolist', JSON.stringify(state.todolist))
		},
	}
})

export default store;
